(function (angular) {

    angular.module('ytong.controllers').controller('resourcesCtrl', resourcesCtrl);

    function resourcesCtrl(resourceManager, ModalService, Constants){

        this.RESOURCES = Constants.RESOURCES;
        this.URLS = Constants.URLS;

        this.trucks = resourceManager.getResource(Constants.RESOURCES.TRUCKS);
        this.cranes = resourceManager.getResource(Constants.RESOURCES.CRANES);
        this.products = resourceManager.getResource(Constants.RESOURCES.PRODUCTS);
        this.areas = resourceManager.getResource(Constants.RESOURCES.AREAS);

        this.getCraneName = function(craneID){
            for(var i=0; i < this.cranes.length; i++){
                if(this.cranes[i].id === craneID){
                    return this.cranes[i].name;
                }
            };

            return "Unknown Crane";
        };

        this.editResource = function(resource, templateURL, url, resourceName){
            ModalService.showModal({
                templateUrl: templateURL,
                controller: 'editResourceControl as erc',
                inputs:{
                    resource: resource,
                    resourceName: resourceName,
                    cranes: this.cranes
                }
            })
                .then(function(modal){
                    modal.element.modal();
                    modal.close.then(function(result){

                        if(result.action === Constants.COMMANDS.ADD){
                            resourceManager.addResource(url, result.resource, resourceName);
                        }else if(result.action === Constants.COMMANDS.UPDATE){
                            resourceManager.updateResource(url, result.resource, resourceName);
                        }else if(result.action === Constants.COMMANDS.DELETE){
                            resourceManager.deleteResource(url, result.resource, resourceName);
                        }
                    });
                });
        };
    };

    angular.module('ytong.controllers').controller('editResourceControl', editResourceControl);

    function editResourceControl(close, resource, resourceName, Constants, cranes){

        this.cranes = cranes;

        if(resource !== undefined){
            this.resource = angular.copy(resource);
        }else{
            this.resource = {};
        }


        if(resourceName === Constants.RESOURCES.AREAS){
            if(resource === undefined){
                this.resource.area_cities = [];
                var city = {};
                this.resource.area_cities.push(city);
            }else{
                this.resource.removed_cities = [];

            }
        };

        if(resourceName === Constants.RESOURCES.PRODUCTS){
            if(resource === undefined){
                this.resource.cranes = [];
                var crane = {};
                this.resource.cranes.push(crane);
            }else{
                this.resource.removed_cranes = [];

            }
        };

        this.isDeleteAllowed = function(){
            return (resource !== undefined);
        };

        this.addAreaCity = function(){
            var city = {};
            this.resource.area_cities.push(city);
        };

        this.removeAreaCity = function($index){
            if(resource !== undefined){
                this.resource.removed_cities.push(angular.copy(this.resource.area_cities[$index]));
            }
            this.resource.area_cities.splice($index, 1);
        };

        this.addProductCrane = function(){
            var crane = {};
            this.resource.cranes.push(crane);
        };

        this.removeProductCrane = function($index){
            if(resource !== undefined){
                this.resource.removed_cranes.push(angular.copy(this.resource.cranes[$index]));
            }
            this.resource.cranes.splice($index, 1);
        };

        this.close = function(action) {
            var result = {};
            result.action = action;
            result.resource = this.resource;
            close(result, 500); // close, but give 500ms for bootstrap to animate
        };

    };
}(angular));